home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / Virtual User tools / SPEC S&L v.1.0.1 / Libraries / String.Lib < prev    next >
Encoding:
Text File  |  1993-12-17  |  12.2 KB  |  354 lines  |  [TEXT/MPS ]

  1. #
  2. # ****************************************************************************
  3. #
  4. #    File Name:        String.Lib
  5. #
  6. #    Contains:    xxx put contents here xxx
  7. #
  8. #    Written by:    Kevin Avoy, Ken Landreth, Michael Leong, Gil Spencer et al
  9. #
  10. #    Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  11. #
  12. # ****************************************************************************
  13. #            C h a n g e        H i s t o r y (most recent first):
  14. # ****************************************************************************
  15. #
  16. #        Vers      Date        Author        Description
  17. #        ----    --------    ------    ---------------------------------------------
  18. #     <1.0.2>     8/20/93    KTA        Added StringUntilChar(), StripCarriageReturn(),
  19. #                                    NumTimesCharInString(), StringAfterChar() - for better filepath
  20. #                                    parsing.
  21. #        <1+>     5/21/93    NAGA        Adding header and porting old files to follow new standards
  22. #
  23. # ****************************************************************************
  24. #
  25.  
  26. #########################################################################
  27. #                             IsSubString(str1, str2)
  28. #=======================================================================
  29. # Author:              SL
  30. # Description:        Checks to see if str1 is in str2.
  31. # Parameters:        str1    :=    Substring to be searched for.
  32. #                    str2    :=    String.
  33. # Returns:            0        :=    str1 is not in str2.
  34. #                    1        :=    str1 is in str2.
  35. #=======================================================================
  36. # History:
  37. #
  38. #########################################################################
  39. TASK IsSubString(str1, str2) begin
  40.      if (str2 = str1) or ( str2 ~= /≈"{str1}"≈/) return(1);
  41.      else return(0);
  42. end; # isSubString()
  43.  
  44. #########################################################################
  45. #                             PointListToStr(pts)
  46. #=======================================================================
  47. # Author:              DM
  48. # Description:        Converts a list of points to a string.
  49. # Parameters:        pt    :=    List of coordinate pairs.
  50. # Returns:            0        :=    if the conversion fails.
  51. #                    1        :=    The pt as a string.
  52. #=======================================================================
  53. # History:
  54. #
  55. #########################################################################
  56. TASK PointListToStr(pointList := {}) begin
  57.     returnVal := O;
  58.     if(pointList) begin
  59.         pointStr := "";
  60.         pointCount := card(pointList);
  61.         for index := 1 to (pointCount-1) begin
  62.             point := pointList[index];
  63.             x := point[1];
  64.             y := point[2];
  65.             pointStr := "{pointStr}∂{{x},{y}∂},";
  66.         end;
  67.         point := pointList[index];
  68.         x := point[1];
  69.         y := point[2];
  70.         pointStr := "∂{{pointStr}∂{{x},{y}∂}∂}";
  71.         returnVal := pointStr;
  72.     end;
  73.     else
  74.         Println "The list passed to PointListToStr is empty";
  75.     return(returnVal);
  76. end; # PtToStr()
  77.  
  78. #########################################################################
  79. #                         FindPos(char, str, startPos)
  80. #=======================================================================
  81. # Author:          SL
  82. # Description:    Finds the first occurence of char in str starting
  83. #                from startPos.
  84. # Parameters:    char    :=    A character to be searched for.
  85. #                str        :=     String.
  86. #                startPos:=    Starting position to be searched.
  87. # Returns:        0        :=    Char is not in str starting from
  88. #                            startPos.
  89. #                pos        :=     Char position of its first occurence
  90. #                            starting from startPos.
  91. #=======================================================================
  92. # History:
  93. #
  94. #########################################################################
  95. TASK FindPos(char, str, startPos:= 1)
  96. begin
  97.     numStr := card str;
  98.     
  99.     # If start position is greater than the string length, return 0 #
  100.     if (startPos > numStr)
  101.         return(0);
  102.     
  103.     for i:= startPos to numStr do
  104.         begin
  105.             if (char = str[i])
  106.                 return(i);
  107.         end; # for
  108.     return(0);
  109. end; # findPos()
  110.  
  111. #########################################################################
  112. #                         Substring(String,StartChar,NumChar)
  113. #=======================================================================
  114. # Author:          PF 
  115. # Description:    returns the first NumChar characters of the passed String
  116. #                starting at StartChar.
  117. # Parameters:    String - String to use
  118. #                StartChar - Character to start making substring from
  119. #                NumChar - Integer number of characters to return
  120. # Returns:        string
  121. # Assumptions:    none
  122. #=======================================================================
  123. # History:
  124. #
  125. ##########################################################################
  126. TASK Substring(String,StartChar,NumChar) begin
  127.     # first see if number of characters is greater then length of string
  128.     # and return the whole end of the string if it is.
  129.     if (NumChar > ((cardString ) - StartChar + 1)) begin
  130.         return (Substring (String,StartChar,((cardString) - StartChar) +1));
  131.         
  132.     end;
  133.     else begin
  134.         # create a string of first NumChar characters and return it.
  135.         NewString:="";                            #start with an empty string
  136.         for CharCount :=StartChar to ((StartChar + NumChar) - 1)
  137.             NewString := NewString + String[CharCount];
  138.             return (NewString);
  139.     end;
  140. end; #Substring Task
  141.  
  142. #########################################################################
  143. #                        RandomString( NumChar )
  144. #========================================================================
  145. # Author:        Kevin Avoy (ext. 45604)
  146. # Description:    Generate Random strings <NumChar> long.
  147. # Parameters:    NumChar := Length of random string to generate
  148. # Returns:        The random string.
  149. # Examples:        RandomString( 5 ); - Random String 5 characters in length
  150. # Assumptions:    That you want Caps, lower case, numbers and punctuation
  151. #========================================================================
  152. # History:
  153. #
  154. #########################################################################
  155. TASK RandomString(NumChar := 3) 
  156. begin
  157.     RandStr := '';
  158.         #FullcharSTRING := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&()_-=+{[}]"';></?`\'~;        # All Caps    
  159.         ALLCAPSString := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';        # ALL CAPS    
  160.         lowercaseString := 'abcdefghijklmnopqrstuvwxyz';    # lower case
  161.         NumberString := '0123456789';                        # Numbers
  162.         PunctuationString := " !@#$%^&()_-∂{∂}'~`";            # Punctuaton
  163.         
  164.         charSTRING := ALLCAPSString + lowercaseString + NumberString + PunctuationString;
  165.     for NumTimes := 1 to NumChar begin                    # How many characters
  166.         WhichItem := Random(1,Card charSTRING);            # Get a random index into the charString
  167.         NewChar := charSTRING[WhichItem];                # Get a character
  168.         RandStr := RandStr + NewChar;                    # Concat to the String
  169.     end;
  170.     Return(RandStr);
  171. end; # RandomString()
  172.  
  173. #############################################################################
  174. TASK FormPositiveInteger(str, start_index := 1, end_index := -1)
  175. begin
  176.     num := 0;
  177.     if (end_index = -1) end_index := card(str);
  178.     for i:= start_index to end_index
  179.     begin
  180.         digit := CharToDigit(str[i]); 
  181.         if (digit = -1) return -1; #error condition
  182.         else if (num > 3276) return -1; #overflow condition
  183.         else if (num = 3276) and (digit > 7) return -1; #overflow condition
  184.         num := num*10 + digit;
  185.     end;#for each digit
  186.     return num;
  187. end;
  188. #############################################################################
  189. TASK CharToDigit(character)
  190. begin
  191.     if (character = '0')
  192.         return (0);
  193.     else if (character = '1')
  194.         return(1);
  195.     else if (character = '2')
  196.         return(2);
  197.     else if (character = '3')
  198.         return(3);
  199.     else if (character = '4')
  200.         return(4);
  201.     else if (character = '5')
  202.         return(5);
  203.     else if (character = '6')
  204.         return(6);
  205.     else if (character = '7')
  206.         return(7);
  207.     else if (character = '8')
  208.         return(8);
  209.     else if (character = '9')
  210.         return(9);
  211.     else return (-1); #error condition
  212. end;
  213.  
  214. #########################################################################
  215. #                    StripCarriageReturn(pLine)
  216. #========================================================================
  217. # Author:        Kevin Avoy (x4-5604)
  218. # Description:    returns a string that contains all of the characters in 
  219. #                <pLine> up to but not including the carriage return.
  220. # Parameters:    pLine - The line of text
  221. # Returns:        string without the carriage return
  222. # Examples:        StripCarriageReturn();
  223. # Assumptions:    None 
  224. #========================================================================
  225. # History:
  226. #
  227. #########################################################################
  228. TASK StripCarriageReturn(pLine)
  229. begin
  230.     return(StringUntilChar(pLine, "∂n",0));
  231. end;
  232.  
  233. #########################################################################
  234. #        StringUntilChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
  235. #========================================================================
  236. # Author:        Kevin Avoy (x4-5604)
  237. # Description:    returns a string that contains all of the characters in 
  238. #                <pTheString> up to <pTheChar>. If <pIncludeTheChar> evaluates to
  239. #                true <pTheChar> will be included in the returned string.
  240. # Parameters:    pTheString - The string  
  241. #                pTheChar - the character to search for
  242. #                pIncludeTheChar - Boolean indicates whether or not to include 
  243. #                                <pTheChar>.
  244. #                pNumOccurences - indicates how many times <pChar> should occur
  245. #                                prior to returning the string.
  246. # Returns:        returns a string that contains all of the characters in 
  247. #                <pTheString> up to (but not necessarily including) <pTheChar>. 
  248. # Examples:        StringUntilChar();
  249. # Assumptions:    None 
  250. #========================================================================
  251. # History:
  252. #
  253. #########################################################################
  254. TASK StringUntilChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
  255. begin
  256.     newString := '';
  257.     actualOccurrence := 0;
  258.     if(pTheString)
  259.     begin
  260.         for i := 1 to Card(pTheString)
  261.         begin
  262.             if not (pTheString[i] = pTheChar)
  263.                     newString := newString + pTheString[i];
  264.             else
  265.             begin
  266.                 actualOccurrence := actualOccurrence +1;
  267.                 if (actualOccurrence = pNumOccurences)
  268.                 begin
  269.                     if(pIncludeTheChar)
  270.                         newString := newString + pTheString[i];
  271.         
  272.                     return (newString);
  273.                 end;
  274.                 else
  275.                     newString := newString + pTheString[i];
  276.             end;
  277.         end;
  278.     end;
  279.     return (newString);
  280. end;
  281.  
  282. #########################################################################
  283. #                NumTimesCharInString(pTheString, pTheChar)
  284. #========================================================================
  285. # Author:        Kevin Avoy (x4-5604)
  286. # Description:    returns an integer that indicates the number of times <pChar>
  287. #                occurs in <pTheString>.
  288. # Parameters:    pTheString - The string  
  289. #                pTheChar - the character to search for
  290. # Returns:        returns an integer that indicates the number of times <pChar>
  291. #                occurs in <pTheString>. 
  292. # Examples:        NumTimesCharInString("hd:thisFolder:ThatFolder:FileName", ":");
  293. # Assumptions:    None 
  294. #========================================================================
  295. # History:
  296. #
  297. #########################################################################
  298. TASK NumTimesCharInString(pTheString, pTheChar)
  299. begin
  300.     actualOccurrence := 0;
  301.     for i := 1 to Card(pTheString)
  302.     begin
  303.         if (pTheString[i] = pTheChar)
  304.             actualOccurrence := actualOccurrence +1;
  305.     end;
  306.     return (actualOccurrence);
  307. end;
  308.  
  309. #########################################################################
  310. #        StringAfterChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
  311. #========================================================================
  312. # Author:        Kevin Avoy (x4-5604)
  313. # Description:    returns a string that contains all of the characters in 
  314. #                <pTheString> after <pTheChar>. If <pIncludeTheChar> evaluates to
  315. #                true <pTheChar> will be included in the returned string.
  316. # Parameters:    pTheString - The string  
  317. #                pTheChar - the character to search for
  318. #                pIncludeTheChar - Boolean indicates whether or not to include 
  319. #                                <pTheChar>.
  320. #                pNumOccurences - indicates how many times <pChar> should occur
  321. #                                prior to creating the returned string. 
  322. # Returns:        returns a string that contains all of the characters in 
  323. #                <pTheString> after <pTheChar>. 
  324. # Examples:        StringAfterChar();
  325. # Assumptions:    None 
  326. #========================================================================
  327. # History:
  328. #
  329. #########################################################################
  330. TASK StringAfterChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
  331. begin
  332.     newString := '';
  333.     actualOccurrence := 0;
  334.     for i := 1 to Card(pTheString)
  335.     begin
  336.         if not(CreateStringFlag)
  337.         begin
  338.             if (pTheString[i] = pTheChar)
  339.             begin
  340.                 actualOccurrence := actualOccurrence +1;
  341.                 if(pNumOccurences = actualOccurrence)
  342.                 begin
  343.                     if(pIncludeTheChar)
  344.                         newString := newString + pTheString[i];
  345.                     CreateStringFlag := 1;
  346.                 end;
  347.             end;
  348.         end;
  349.         else
  350.             newString := newString + pTheString[i];
  351.     end;
  352.     return (newString);
  353. end;
  354.